// pages/patient/save.js
var app = getApp(),
  a = app.requirejs("core");
Page({

  /**
   * 页面的初始数据
   */
  data: {

    array1: ['blue', 'green', 'red', 'black', 'orange'],
    array2: ['蓝色风格', '绿色风格', '红色风格', '黑色风格', '橘黄色风格'],
    index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this,
      i = getApp().getCache("userinfo");
    if (i) {
      a.get('Shop/info', {
        uid: i.uid
      }, function (t) {
        if (t.code == 0) {
          var index=0;
          that.data.array1.forEach(function(k,i){
            if (k == t.info.shop.card_color){
              index=i;
            }
          })
          that.setData({
            info: t.info,
            latitude: t.info.shop.latitude,
            longitude: t.info.shop.longitude,
            current_address: t.info.shop.address,
            index:index,
            show: true
          })
        } else {
          a.alert(t.msg);
        }
      }, !that.data.show)
    } 
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
  * 选择位置位置
  */
  chooseLocation: function (e) {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        // success
        console.log(res)
        that.setData({
          current_address: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
      fail: function () {
        //  a.alert('未获取到权限，调起地图失败')
      }
    })
  },
  /**
     * 修改病人信息
     */
  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    if (data.name == '') {
      a.error("请填写名称")
      return;
    }
    if (data.phone == '') {
      a.error("请填写手机号")
      return;
    }
    if (data.address == '') {
      a.error("请选择地址")
      return;
    }
    data.uid = getApp().getCache("userinfo").uid;
    data.card_color = that.data.array1[that.data.index];
    console.log(data);
    a.get('shop/add_shop',
      data, function (t) {
        console.log(t)
        if (t.code == 0) {
          //成功操作
          a.success('操作成功');
          setTimeout(function () {
            app.setCache('shop_info',1314,3)
            a.jump('/pages/index/index', 4);
          }, 1000)

        } else {
          //失败提示
          a.alert(t.msg)
        }
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})